const path = require('path');
const BaseIpcHandler = require('../BaseIpcHandler');

class SaveLabelHandler extends BaseIpcHandler {
    async handle(event, params) {
        // 解析参数
        // console.log('>> SaveLabelHandler params = ', params);
        const { dataset_supertype, dataset_type, label_format } = params;

        // 业务逻辑
        let result = null;
        try {
            const loadersDir = path.join(__dirname, '..', '..', 'labels', dataset_supertype, dataset_type);
            const loaderFile = label_format.toUpperCase() + 'LabelLoader.js';
            const loaderClass = require(path.join(loadersDir, loaderFile));
            const loader = new loaderClass(this.app);
            result = await loader.save(params);
            result = { code: 0, msg: '保存标注结果成功！' };
        } catch (error) {
            console.error('Error SaveLabel, Error:', error);
            result = { code: -1, msg: error.message };
        }

        // 返回结果
        // console.log(`SaveLabelHandler result = `, result);
        if (event.from === 'localserver') {
            event.resp.send(result);
        } else {
            return result;
        }
    }
}

module.exports = SaveLabelHandler;
